import core
import os
import time
import uuid
import json
import re
from pathlib import Path
def validate_path(path: str) -> str:
wiki_root = Path(core.data_directory + "/wiki")
requested_path = Path(path).resolve()
if wiki_root not in requested_path.parents:
exit(0)
return path
def read_page(name, process_links = True, sub_new_line = False):
global current_session
try:
with open(validate_path(core.data_directory + '/wiki/' + name + '.json'), 'r') as page_file:
page = json.load(page_file)
complete = page['content']
if process_links == True:
process_links = re.sub(r'(::[A-Za-z0-9]+)', r'\1', page['content'])
complete = re.sub('::', '', process_links)
if sub_new_line:
complete = re.sub('n', '***', complete)
page['content'] = complete
return page
except:
content = 'Try again.'
if current_session:
content = ' Add Page '
return {'title': 'Page not found or other error.',
'content': content,
'author': '',
'time': 0.0
}
def delete_page(title):
os.remove(validate_path(core.data_directory + '/wiki/' + title + '.json'))
def add_page(author, title, content):
page_uuid = str(uuid.uuid4())
content = re.sub('***', 'n', content)
page = {'author': author, 'title': title, 'content': content, 'id': page_uuid, "time": time.time()}
page_object = json.dumps(page, indent=4)
with open(validate_path(core.data_directory + '/wiki/' + title + '.json'), "w") as page_file:
page_file.write(page_object)
if 'link_id' not in os.environ:
os.environ['link_id'] = 'local_test'
current_session = core.get_current_session(os.environ['link_id'])
core.header(current_session)
if current_session:
if 'var_add_page' in os.environ:
print()
print('To link to a page, or create a new page, use a double collon. For example: ::NewPagenThree asterisks in a row (***) are equivelent to a newline.')
print()
print('Name: ')
print()
print('Content: ')
print()
elif 'var_delete_page' in os.environ:
print('Page deleted.')
delete_page(os.environ['var_delete_page'])
elif 'var_save_page' in os.environ:
print('Page saved.')
try:
add_page(current_session['username'], os.environ['field_pagename'], os.environ['field_content'])
except Exception as e:
print(str(e))
elif 'var_edit_page' in os.environ:
page = read_page(os.environ['var_edit_page'], process_links = False, sub_new_line = True)
print()
print('To link to a page, or create a new page, use a double collon. For example: ::NewPagenThree asterisks in a row (***) are equivelent to a newline.')
print()
print('Name: ')
print()
print('Content: ')
print()
if 'var_page_name' not in os.environ:
os.environ['var_page_name'] = 'Index'
if 'var_page_name' in os.environ and 'var_edit_page' not in os.environ and 'var_save_page' not in os.environ and 'var_add_page' not in os.environ and 'var_delete_page' not in os.environ:
page = read_page(os.environ['var_page_name'])
if current_session and page['title'] != 'Page not found or other error.':
try:
except Exception as e:
print(str(e))
print(top_buttons)
print('' + page['title'] + '')
print('Author: ' + '<' + page['author' + '>' + core.page_path + '/profile.musername=' + page['author'] + ']')
print('Last updated: ' + core.convert_time(page['time']))
print('-')
print(page['content'])
print('')
print()
core.footer()